﻿using DLL.Models.RequestEmployeeModels;
using DLL.Models;
using DLL.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Deptus.TestEncounter.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class JobController : ControllerBase
    {
        private IEmployeeRepository EmployeeRepository;
        private IShiftRepositiry ShiftRepositiry;
        private IJobRepository JobRepository;

        public JobController(IEmployeeRepository employeeRepository, IShiftRepositiry shiftRepositiry, IJobRepository jobRepository)
        {
            EmployeeRepository = employeeRepository;
            ShiftRepositiry = shiftRepositiry;
            JobRepository = jobRepository;
        }

        [HttpGet("GetById{id}")]
        public async Task<IActionResult> GetById([FromRoute] int id)
        {
            var result = await JobRepository.GetById(id);
            if (result == null)
            {
                return BadRequest();
            }

            return Ok(result);
        }

        [HttpPost("CreateNew")]
        public async Task<IActionResult> CreateNew([FromBody] JobPosition jobPosition)
        {
            try
            {

                var jobPositions = await JobRepository.GetById(jobPosition.Id);

                if (jobPositions != null)
                    throw new Exception("Уже есть такой ID должности");

                await JobRepository.Create(jobPosition);

                return Ok(jobPosition);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }

        [HttpPatch("Update{id}")]
        public async Task<IActionResult> Update([FromRoute] int id, [FromBody] string updateJobPosition)
        {
            try
            {
                var item = await JobRepository.GetById(id);
                if (item == null)
                    throw new Exception("Нет такой должности");

                item.JobPositionName = updateJobPosition;       
                await JobRepository.Update(item);

                return Ok(item);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }

        [HttpDelete("Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await JobRepository.GetById(id);
            if (result == null)
            {
                return BadRequest();
            }
            try
            {
                await JobRepository.Delete(result);
            }
            catch (Exception ex) { return BadRequest(ex.Message); }
            return Ok(result);
        }

        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var result = await JobRepository.GetAll();
            if (result == null)
            {
                return BadRequest();
            }
            return Ok(result);
        }



    }
}
